import $ from 'jquery';
import hostaddress from './hostaddress.js';

function RESTClient(options){
  this.hostname=hostaddress.host;
  if(options && options.hostname){
    this.hostname= options.hostname;
  }
}

RESTClient.prototype={
    get:function (path,data,type,onSuccess,onError){
      var hostname=this.hostname;
      if(type==="xcx"){
        hostname=hostaddress.serverHost;
      }
      var url=hostname+path;
    	$.ajax({
    		  url: url,
          crossDomain:true,
    		  dataType: 'json',
    		  cache: false,
    		  success: onSuccess,
          data: data,
    		  error: function(xhr, status, err) {
    		    console.error("xhr=",xhr,"status=",status,"error=", err);
    		  }
    		});
    },
    post:function (path,data,type,onSuccess,onError){
        var hostname=this.hostname;
        if(type==="xcx"){
          hostname=hostaddress.serverHost;
        }
        var url=hostname+path;
    	 $.ajax({
          url: url,
          crossDomain:true,
          dataType: 'json',
          type: 'POST',
          data: data,
          success: onSuccess,
          error: function(xhr, status, err) {
            console.error("error url:"+url,"status=",status,"error=", err);
          }
        });
    },
    Login:function(data,onSuccess, onError){
      this.post("backstageLogin",data,'xcx',onSuccess,onError);
    },
    XCXQuery:function(data,onSuccess, onError){
      this.post("xcx/query",data,'xcx',onSuccess,onError);
    },
    XCXSetStatus:function(data,onSuccess, onError){
      this.post("xcx/setStatus",data,'xcx',onSuccess,onError);
    }
}
export default RESTClient;
